package com.geralt.im.tool;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class StringtoBean {
	
	public static void setObject(Object obj,Map<String,String> map){
		for(String key:map.keySet()){
			String value = map.get(key);
			setAttribute(obj, key,value);
		}
	}
	
	
	public static void setAttribute(Object obj, String key, String value) {
		try {
			Class<?> clz = obj.getClass();
			Field field = clz.getDeclaredField(key);
			Class<?> k = field.getType();
			String tmpString = Character.toUpperCase(key.charAt(0))
					+ key.substring(1);
			String methodName = "set" + tmpString;
			Method method = clz.getMethod(methodName, k);
			if (String.class == k)
				method.invoke(obj, value);
			else if (Integer.class == k) {
				Integer i = Integer.parseInt(value);
				method.invoke(obj, i);
			} else if (java.util.Date.class == k) {
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy/MM/dd hh:mm:ss");
				Date kDate = sdf.parse(value);
				method.invoke(obj, kDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
